package com.example.back.service.impl;

import com.example.back.dto.req.OperatLogReq;
import com.example.back.dto.resp.OperatLogResp;
import com.example.back.mapper.OperatLogMapper;
import com.example.back.service.OperatLogService;
import com.example.common.entity.RoleModules;
import com.example.common.enums.EnumHelperCode;
import com.example.common.support.ApiException;
import com.example.common.utils.CheckUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import org.javers.core.Javers;
import org.javers.core.changelog.SimpleTextChangeLog;
import org.javers.core.diff.Change;
import org.javers.core.metamodel.object.CdoSnapshot;
import org.javers.repository.jql.QueryBuilder;

import javax.annotation.Resource;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OperatLogServiceImpl implements OperatLogService {


    @Resource
    private OperatLogMapper operatLogMapper;
    @Resource
    private Javers javers;

    /**
     * 获取日志审计列表
     * @param req
     * @return
     */
    @Override
    public String[] list(OperatLogReq req) {
      Instant instant = req.getBeg().toInstant();
      ZoneId zoneId = ZoneId.systemDefault();
      LocalDate beg = instant.atZone(zoneId).toLocalDate();
      
      Calendar ca = Calendar.getInstance();
      ca.setTime(req.getEnd());
      ca.add(Calendar.DAY_OF_MONTH, 1);
      req.setEnd(ca.getTime());        
      Instant instantend = req.getEnd().toInstant();
      ZoneId zoneIdend = ZoneId.systemDefault();
      LocalDate end = instantend.atZone(zoneIdend).toLocalDate();

      QueryBuilder jqlQuery = QueryBuilder.anyDomainObject().from(beg).to(end);
      List<Change> changes = javers.findChanges(jqlQuery.build());
      
      String changeLog = javers.processChangeList(changes, new SimpleTextChangeLog());
      String[] log = changeLog.split("commit");
      
      return log;
    }
}
